﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Dhtl.Service.IService;
using dai_hoc_tuong_lai.Areas.Admin.Models;
using DataBase.Daihoctuonglai;
using FarmeWork.Helper;
using Comom.Project.ControllerCom;

namespace dai_hoc_tuong_lai.Areas.Admin.Controllers
{
     [Authorize(Roles = "Admin,employee")]
    public class EmailSystemController : UTController
    {
        //
        // GET: /Admin/EmailSystem/

        private readonly IEmailSystem _iEmailSystem;

        public EmailSystemController(IEmailSystem iEmailSystem)
        {
            this._iEmailSystem = iEmailSystem;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ListEmail()
        {

            var listEntity = _iEmailSystem.List();
            List<EmailSyemModel> model = new List<EmailSyemModel>();
            try
            {
                foreach (var item in listEntity)
                {
                    EmailSyemModel ml = new EmailSyemModel();
                    EmailSyemModel.MapFrom(item, ref  ml);
                    model.Add(ml);
                }
            }
            catch { }

            return PartialView("_ListEmail",model);
        }

        public ActionResult Add()
        {
            EmailSyemModel model = new EmailSyemModel();
            model.Id = 0;
            model.DateCreate = DateTime.Now;
            model.Status = true;
            model.type = true;
            return PartialView("_Add", model);
        }

        public ActionResult Update(int id)
        {
            EmailSyemModel model = new EmailSyemModel();
            var entity = _iEmailSystem.GetById(id);
            try
            {
                EmailSyemModel.MapFrom(entity, ref model);
            }
            catch { }

            return PartialView("_Update", model);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddOrUpdate(EmailSyemModel model)
        {

            try
            {
                if (string.IsNullOrEmpty(model.Email))
                {
                    return JsonError("Vui lòng điền Email");
                }

                if (!IsValidEmailAddress(model.Email))
                {
                    return JsonError("Email bạn nhập sai định dạng");
                }

                if (string.IsNullOrEmpty(model.Pass))
                {
                    return JsonError("Nhập Password vào");
                }

                var entity = new EmailSystem();
                EmailSyemModel.MapFrom(model, ref entity);
                _iEmailSystem.AddOrUpdate(entity);

                return JsonSuccess(Url.Action("Index"), null);
            }
            catch (Exception ex)
            {
                return JsonError(ex.ToString());
            }
        }

        public ActionResult Delete(int id)
        {
            try
            {
                _iEmailSystem.delete(id);
                return JsonSuccess(Url.Action("Index"), null);
            }
            catch (Exception ex)
            {
                return JsonError(ex.ToString());
            }
        }
    }
}
